package com.changwu.cwbase.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.changwu.cwbase.Controllers;

import org.apache.mina.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * comments
 * 
 * @author zhuft
 * @version 1.0
 */
public class PreferencesUtil {

	/** 保存一些信息 */
	private static SharedPreferences mPreference;

	/**
	 * 初始化
	 *
	 * @param context
	 */
	public static void ensureInit(Context context) {
		if (mPreference == null) {
			mPreference = context.getSharedPreferences(Controllers.preferencesName, Context.MODE_PRIVATE);
		}
	}

	public static void saveBooleanData(String key, boolean value) {
		SharedPreferences.Editor editor = mPreference.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	public static void saveFloatData(String key, float value) {
		SharedPreferences.Editor editor = mPreference.edit();
		editor.putFloat(key, value);
		editor.commit();
	}
	public static void saveStringData(String key, String value) {
		SharedPreferences.Editor editor = mPreference.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static void saveIntData(String key, int value) {
		SharedPreferences.Editor editor = mPreference.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static String getStringData(String key, String defaultValue) {
		return mPreference.getString(key, defaultValue);
	}
	public static float getFloatData(String key, float defaultValue) {
		return mPreference.getFloat(key, defaultValue);
	}
	public static boolean getBooleanData(String key, boolean defaultValue) {
		return mPreference.getBoolean(key, defaultValue);
	}

	public static int getIntData(Context activity, String key, int defaultValue) {
		return mPreference.getInt(key, defaultValue);
	}
	/**
	 * @方法说明:保存对象
	 * @方法名称:savaObject
	 * @param key
	 * @param ob
	 * @返回void
	 */
	public static boolean savaObject( String key, Object ob)
	{
		if (ob == null)
		{
			return false;
		}
		boolean falg = false;
		// 创建字节输出
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;
		try
		{
			// 创建对象输出流，并封装字节流
			oos = new ObjectOutputStream(baos);
			// 将对象写入字节流
			oos.writeObject(ob);
			// 将字节流编码成base64的字符窜
			String oAuth_Base64 = new String(Base64.encodeBase64(baos.toByteArray()));
			falg = mPreference.edit().putString(key, oAuth_Base64).commit();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (oos != null)
				{
					oos.close();
				}

				if (baos != null)
				{
					baos.close();
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			return falg;
		}
	}

	/**
	 * @方法说明:获取存储的对 * @方法名称:getObject
	 * @param key
	 * @return
	 * @返回Object
	 */
	public static Object getObject( String key)
	{
		Object ob = null;
		String productBase64 = mPreference.getString(key, "");
		// 读取字节
		byte[] base64 = Base64.decodeBase64(productBase64.getBytes());
		// 封装到字节流
		ByteArrayInputStream bais = new ByteArrayInputStream(base64);
		ObjectInputStream bis = null;
		try
		{
			// 再次封装
			bis = new ObjectInputStream(bais);
			// 读取对象
			ob = bis.readObject();
		}
		catch (Exception e)
		{
			// e.printStackTrace();
		}
		finally
		{
			try
			{
				if (bis != null)
				{
					bis.close();
				}

				if (bais != null)
				{
					bais.close();
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		if(ob==null){

		}
		return ob;
	}
}